/* eslint-disable react/no-unstable-nested-components */
/* eslint-disable no-shadow */
/* eslint-disable react-native/no-inline-styles */
import React from 'react';
import {DeviceEventEmitter, TouchableOpacity} from 'react-native';
import {Text, View, Colors, Card, Button} from 'react-native-ui-lib';
import FontAwesome from 'react-native-vector-icons/FontAwesome';
import {get, del} from '@/utils/request';
import * as Progress from 'react-native-progress';
import Clipboard from '@react-native-clipboard/clipboard';

const taskobj = {
  1: {
    text: '未提交',
    color: '#999999',
    says: '创建时间',
  },
  2: {
    text: '审批中',
    color: '#4DA7F3',
    says: '提交时间',
  },
  3: {
    text: '不通过',
    color: '#ff4800',
    says: '驳回时间',
  },
  4: {
    text: '通过',
    color: '#3F9F3F',
    says: '审批时间',
  },
};

let ReportItem = ({it, navigation, setAction, state, setState}) => {
  const iftype = it?.type === 1;
  const ifs = it?.remark && it?.refuse;
  return (
    <Card
      key={it?.id}
      flex
      enableShadow={false}
      padding-12
      paddingB-4
      marginH-12
      marginB-12
      containerStyle={{
        backgroundColor: Colors.colorBgSecondary,
      }}
      onPress={() => {
        setAction(s => ({
          ...s,
          visible: true,
          title: '日报操作',
          options: [
            {
              label: '编辑',
              onPress: async () => {
                navigation.navigate('FormPage', {
                  title: '编辑日报',
                  type: 'EidtReport',
                  navigation,
                  defaultValue: it,
                  iftype,
                });
              },
            },
            {
              label: '删除',
              onPress: async () => {
                DeviceEventEmitter.emit('dialog', {
                  title: '是否删除该日报记录',
                  type: 'bottom',
                  content: '删除后需要从任务中重新添加工时，是否立即删除?',
                  height: 120,
                  action: set => (
                    <Button
                      label="立即删除"
                      size="small"
                      style={{
                        borderRadius: 6,
                        width: 88,
                        backgroundColor: Colors.primaryColor,
                      }}
                      onPress={async () => {
                        let res = await del('/itemWorkTime/' + it?.id);
                        if (res?.code === 0) {
                          let newItems = {
                            ...state.items,
                            [it?.day]: state.items[it?.day]?.filter(
                              itz => itz.id !== it.id,
                            ),
                          };
                          setState(s => {
                            return {
                              ...s,
                              items: newItems,
                            };
                          });

                          DeviceEventEmitter.emit('toast', {
                            message: '删除成功！',
                            type: 'success',
                          });
                          set(s => ({
                            ...s,
                            visible: false,
                          }));
                        }
                      }}
                    />
                  ),
                });
              },
            },
          ],
        }));
      }}>
      <View>
        <View spread centerV row>
          <Text style={{fontSize: 12, color: taskobj[it?.status]?.color}}>
            {taskobj[it?.status]?.text}{' '}
            {it?.projectName ? `| ${it?.projectName}` : ''}
          </Text>

          {it.itemType === 2 && (
            <FontAwesome name={'bug'} size={14} color={Colors.$iconDanger} />
          )}
        </View>
        <View marginV-12 style={{gap: 6}}>
          <View row gap={6} centerV>
            {iftype && (
              <Progress.Circle
                size={24}
                progress={it?.progress / 100}
                color={Colors.primaryColor}
                showsText={false}
                textStyle={{
                  fontSize: 8,
                }}
              />
            )}

            {iftype && (
              <TouchableOpacity
                onPress={() => {
                  DeviceEventEmitter.emit('toast', {
                    message: it.missionNo + '已复制！',
                    type: 'success',
                  });
                  Clipboard.setString(it.missionNo);
                }}>
                <Text body style={{color: Colors.colorTextBase}}>
                  {it.missionNo}
                </Text>
              </TouchableOpacity>
            )}

            {!iftype ? (
              <Text
                flex-1
                subheading
                numberOfLines={1}
                style={{fontWeight: 'bold'}}>
                自定义日报
              </Text>
            ) : (
              <Text
                flex-1
                subheading
                numberOfLines={1}
                style={{fontWeight: 'bold'}}>
                {it.itemName}
              </Text>
            )}
          </View>

          <Text body>
            消耗
            <Text color={Colors.primaryColor} heading marginH-2>
              {it?.workTime}
            </Text>
            小时
          </Text>
          {ifs && (
            <View
              borderTopWidth={0.5}
              borderColor={Colors?.backgroundColor}
              paddingT-6
              gap={6}
              marginT-6>
              {it?.remark && (
                <Text body numberOfLines={2}>
                  备注：{it?.remark}
                </Text>
              )}
              {it?.refuse && (
                <Text body numberOfLines={2} color={taskobj[it?.status]?.color}>
                  拒绝：{it?.refuse}
                </Text>
              )}
            </View>
          )}
        </View>
      </View>
    </Card>
  );
};

export default ReportItem;
